package org.example.contractFinance.controller;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.example.contractFinance.domain.ContractFinance;
import org.example.contractFinance.service.IContractFinanceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同财务Controller
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-20
 */
@Controller
@RequestMapping("/contractFinance/contractFinance")
public class ContractFinanceController extends BaseController
{
    private String prefix = "contractFinance/contractFinance";

    @Autowired
    private IContractFinanceService contractFinanceService;

    @GetMapping()
    public String contractFinance(String accountId,Integer type,String contractId,ModelMap mmp)
    {
        mmp.put("accountId",accountId);
        mmp.put("type",type);
        mmp.put("contractId",contractId);
        return prefix + "/contractFinance";
    }

    /**
     * 查询合同财务列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ContractFinance contractFinance)
    {
        startPage();
        List<ContractFinance> list = contractFinanceService.selectContractFinanceList(contractFinance);
        return getDataTable(list);
    }

    /**
     * 导出合同财务列表
     */
    @RequiresPermissions("contractFinance:contractFinance:export")
    @Log(title = "合同财务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ContractFinance contractFinance)
    {
        List<ContractFinance> list = contractFinanceService.selectContractFinanceList(contractFinance);
        ExcelUtil<ContractFinance> util = new ExcelUtil<ContractFinance>(ContractFinance.class);
        return util.exportExcel(list, "合同财务数据");
    }

    /**
     * 新增合同财务
     */
    @GetMapping("/add")
    public String add(String accountId,Integer type,String contractId,ModelMap mmap)
    {
        mmap.put("accountId",accountId);
        mmap.put("type",type);
        mmap.put("contractId",contractId);
        return prefix + "/add";
    }

    /**
     * 新增保存合同财务
     */
    @RequiresPermissions("contractFinance:contractFinance:add")
    @Log(title = "合同财务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ContractFinance contractFinance)
    {
        contractFinance.setFinanceNo(getContractFinanceNO(contractFinance));
        return toAjax(contractFinanceService.insertContractFinance(contractFinance));
    }

    private String getContractFinanceNO(ContractFinance contractFinance) {
        contractFinance.setCreateTime(DateUtils.getNowDate());
        startPage();
        List<ContractFinance> list = contractFinanceService.selectContractFinanceList(contractFinance);
        TableDataInfo dataTable = getDataTable(list);
        Long total =dataTable.getTotal()+1L;
        String s2 = String.format("%04d", total);
        //收款单：RN2404070001
        //付款单：PN2404070001
        String label ="1".equals(contractFinance.getType())?"RN":"PN";
        String contractFinanceNO =  label+DateUtils.dateTimeNow("yyMMdd")+s2;
        return contractFinanceNO;
    }
    /**
     * 修改合同财务
     */
    @RequiresPermissions("contractFinance:contractFinance:edit")
    @GetMapping("/edit/{financeId}")
    public String edit(@PathVariable("financeId") Long financeId, ModelMap mmap)
    {
        ContractFinance contractFinance = contractFinanceService.selectContractFinanceByFinanceId(financeId);
        mmap.put("contractFinance", contractFinance);
        return prefix + "/edit";
    }

    /**
     * 修改保存合同财务
     */
    @RequiresPermissions("contractFinance:contractFinance:edit")
    @Log(title = "合同财务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ContractFinance contractFinance)
    {
        return toAjax(contractFinanceService.updateContractFinance(contractFinance));
    }

    /**
     * 删除合同财务
     */
    @RequiresPermissions("contractFinance:contractFinance:remove")
    @Log(title = "合同财务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(contractFinanceService.deleteContractFinanceByFinanceIds(ids));
    }
}
